package com.youku.plugins.ads
{
   import com.youku.interfaces.IPlugin;
   import flash.display.MovieClip;
   import com.youku.interfaces.IPlayerProxy;
   import com.youku.adSDK.YoukuAdManager;
   import com.youku.adSDK.ADContext;
   import com.youku.events.PlayerControlEvent;
   import com.youku.events.PluginEvent;
   import com.youku.events.CoreEvent;
   import com.youku.events.ConductorEvent;
   import com.youku.adSDK.YoukuAdEvent;
   import flash.events.Event;
   import com.youku.PlayerConfig;
   import com.youku.utils.ReportUtil;
   import com.youku.events.ReportEvent;
   import com.youku.data.LoadTimeData;
   import com.youku.data.PlayerConstant;
   
   public class ADFacade extends Object implements IPlugin
   {
      
      public function ADFacade() {
         super();
      }
      
      private static var _instance:ADFacade;
      
      public static var frontPlaying:Boolean = false;
      
      public static var frontOverTime:Number = -1;
      
      public static function getInstance() : ADFacade {
         if(_instance == null)
         {
            _instance = new ADFacade();
         }
         return _instance as ADFacade;
      }
      
      private var _mainMc:MovieClip;
      
      private var _playerProxy:IPlayerProxy;
      
      private var _adManager:YoukuAdManager;
      
      private var _adContext:ADContext;
      
      public function init(param1:MovieClip, param2:IPlayerProxy) : void {
         this._mainMc = param1;
         this._playerProxy = param2;
         this._adContext = new ADContext();
         this._adContext.mainMc = this._mainMc;
         this._adManager = YoukuAdManager.getInstance(this._adContext);
         this.addEvents();
      }
      
      private function addEvents() : void {
         this._playerProxy.addEventListener(PlayerControlEvent.REQUEST_AND_PLAY_FRONT_AD,this.onPlayerRequestPlayAD);
         this._playerProxy.addEventListener(PlayerControlEvent.PLAYER_SIZE_CHANGED,this.onPlayerSizeChanged);
         this._playerProxy.addEventListener(PlayerControlEvent.FULLSCREEN_CHANGED,this.onPlayerSizeChanged);
         this._playerProxy.addEventListener(PlayerControlEvent.COMMON_CHANGE_VOLUME,this.onPlayerChangeVolume);
         this._playerProxy.addEventListener(PlayerControlEvent.COMMON_ERROR,this.onPlayerError);
         this._playerProxy.addEventListener(PlayerControlEvent.COMMON_RESET,this.onPlayerReset);
         this._playerProxy.addEventListener(PlayerControlEvent.REQUEST_AND_PLAY_BACK_AD,this.onPlayerShowBackAD);
         this._playerProxy.addEventListener(PlayerControlEvent.COMMON_START_TO_PLAY,this.onCommonStartPlay);
         this._playerProxy.addEventListener(PlayerControlEvent.PANEL_STATE_CHANGE,this.onPanelStateChange);
         this._playerProxy.addEventListener(PlayerControlEvent.VIDEO_END,this.onPlayerOver);
         this._playerProxy.addEventListener(PlayerControlEvent.CORE_CHANGE_SCREEN_MODE,this.onPlayerChangeMode);
         this._playerProxy.addEventListener(PluginEvent.CONTROL_BAR_HIDE,this.onBarHide);
         this._playerProxy.addEventListener(PluginEvent.CONTROL_BAR_SHOW,this.onBarShow);
         this._playerProxy.addEventListener(PluginEvent.CONTROL_BAR_INFO_BAR_SHOW,this.onInformationBarShow);
         this._playerProxy.addEventListener(PluginEvent.CONTROL_BAR_INFO_BAR_HIDE,this.onInformationBarHide);
         this._playerProxy.addEventListener(CoreEvent.PAUSE,this.onCorePause);
         this._playerProxy.addEventListener(CoreEvent.RESUME,this.onCoreResume);
         this._playerProxy.addEventListener(CoreEvent.PLAYING,this.onPlayerPlaying);
         this._playerProxy.addEventListener(ConductorEvent.METADATA_UPDATED,this.onConductorMetadataUpdated);
         this._adManager.addEventListener(YoukuAdEvent.AD_FRONT_START_TO_PLAY,this.onAdFrontStartToPlay);
         this._adManager.addEventListener(YoukuAdEvent.AD_FRONT_LOAD_COMPLETE,this.onAdFrontLoadComplete);
         this._adManager.addEventListener(YoukuAdEvent.AD_FRONT_READY_OR_ERROR,this.onAdFrontReadyOrError);
         this._adManager.addEventListener(YoukuAdEvent.AD_INSERT_REQUEST_TO_PLAY,this.onAdInsertRequestToPlay);
         this._adManager.addEventListener(YoukuAdEvent.AD_INSERT_WILL_PLAY,this.onAdInsertWillPlay);
         this._adManager.addEventListener(YoukuAdEvent.AD_INSERT_POINT_LOADED,this.onAdInsertPointLoaded);
         this._adManager.addEventListener(YoukuAdEvent.AD_PAUSE_HIDE,this.onAdPauseHide);
         this._adManager.addEventListener(YoukuAdEvent.AD_PAUSE_PLAY_BUTTON_CLICK,this.onAdPausePlayBtnClick);
         this._adManager.addEventListener(YoukuAdEvent.AD_PAUSE_NULL,this.onAdPauseNull);
         this._adManager.addEventListener(YoukuAdEvent.AD_VIDEO_PLAY_COMPLETE,this.onAdVideoPlayComplete);
         this._adManager.addEventListener(YoukuAdEvent.AD_VIDEO_PAID_SHOW,this.onAdVideoPaidShow);
         this._adManager.addEventListener(YoukuAdEvent.AD_HOLDER_SHOW,this.onAdHolderShow);
         this._adManager.addEventListener(YoukuAdEvent.AD_HOLDER_SHOW_IN_FRONT,this.onAdHolderShowInFront);
         this._adManager.addEventListener(YoukuAdEvent.AD_SOUND_MUTE,this.onAdSoundMute);
         this._adManager.addEventListener(YoukuAdEvent.AD_LOAD_TIME,this.onAdLoadTimeReport);
         this._adManager.addEventListener(YoukuAdEvent.AD_LOAD_TIME_REQUEST,this.onAdLoadTimeReport);
         this._adManager.addEventListener(YoukuAdEvent.AD_USER_ACTION,this.onAdUserAction);
         this._adManager.addEventListener(YoukuAdEvent.AD_BLOCK_NOTICE_REPORT,this.onAdBlockNoticeReport);
         this._adManager.addEventListener(YoukuAdEvent.AD_BLOCK_REPORT,this.onAdBlockReport);
         this._adManager.addEventListener(YoukuAdEvent.AD_GET_FOCUS,this.onAdGetFocus);
         this._adManager.addEventListener(YoukuAdEvent.AD_LOST_FOCUS,this.onAdLostFocus);
         this._adManager.addEventListener(YoukuAdEvent.AD_SKIP_SHOW,this.onAdSkipShow);
      }
      
      private function onAdSkipShow(param1:Event) : void {
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.AD_SKIP_SHOW));
      }
      
      private function onAdHolderShowInFront(param1:YoukuAdEvent) : void {
         this._playerProxy.dispatchEvent(new PlayerControlEvent(PlayerControlEvent.HIDE_LOADING));
      }
      
      private function onAdGetFocus(param1:YoukuAdEvent) : void {
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.AD_GET_FOCUS));
      }
      
      private function onAdLostFocus(param1:YoukuAdEvent) : void {
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.AD_LOST_FOCUS));
      }
      
      private function onAdPausePlayBtnClick(param1:YoukuAdEvent) : void {
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.AD_REQUEST_TO_RESUME));
      }
      
      private function onAdPauseHide(param1:YoukuAdEvent) : void {
         this._playerProxy.dispatchEvent(new PlayerControlEvent(PlayerControlEvent.HIDE_SHARE,param1.data));
      }
      
      private function onAdPauseNull(param1:YoukuAdEvent) : void {
      }
      
      private function onPanelStateChange(param1:PlayerControlEvent) : void {
         this._adManager.setPlayerPanelStateChange(param1.data.havePanel);
      }
      
      private function onCommonStartPlay(param1:PlayerControlEvent) : void {
         this._adManager.setPlayerStartPlay();
      }
      
      private function onPlayerShowBackAD(param1:PlayerControlEvent) : void {
         this._adManager.loadBack();
      }
      
      private function onAdInsertWillPlay(param1:YoukuAdEvent) : void {
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.AD_SHOW_INSERT_AD_TIP));
      }
      
      private function onAdInsertRequestToPlay(param1:YoukuAdEvent) : void {
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.AD_REQUEST_TO_PLAY));
      }
      
      private function onAdInsertPointLoaded(param1:YoukuAdEvent) : void {
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.AD_ADPOINT_LOADED,param1.data));
      }
      
      private function onCorePause(param1:CoreEvent) : void {
         this._adManager.setPlayerPause(param1.data.useraction);
      }
      
      private function onCoreResume(param1:CoreEvent) : void {
         this._adManager.setPlayerResume();
      }
      
      private function onConductorMetadataUpdated(param1:ConductorEvent) : void {
         var _loc2_:Array = null;
         if(PlayerConfig.isTudouPlayer)
         {
            return;
         }
         if((this._playerProxy.playerData) && (this._playerProxy.playerData.dvdinfo))
         {
            _loc2_ = this._playerProxy.playerData.dvdinfo.insertPoints;
            if(_loc2_)
            {
               this._adManager.setInsertPoints(_loc2_);
            }
         }
      }
      
      private function onAdUserAction(param1:YoukuAdEvent) : void {
         ReportUtil.sendUserActionLog(param1.data as String);
      }
      
      private function onAdVideoPaidShow(param1:YoukuAdEvent) : void {
         this._playerProxy.dispatchEvent(new PlayerControlEvent(PlayerControlEvent.BEFORE_SHOW_PANELS,param1.data));
      }
      
      private function onPlayerChangeMode(param1:PlayerControlEvent) : void {
         this._adManager.setScreenMode(param1.data.mode,param1.data.tween);
      }
      
      private function onBarHide(param1:PluginEvent) : void {
         this._adManager.setControlbarHide();
      }
      
      private function onBarShow(param1:PluginEvent) : void {
         this._adManager.setControlbarShow();
      }
      
      private function onInformationBarShow(param1:PluginEvent) : void {
         this._adManager.setInfobarShow();
      }
      
      private function onInformationBarHide(param1:PluginEvent) : void {
         this._adManager.setInfobarHide();
      }
      
      private function onAdSoundMute(param1:YoukuAdEvent) : void {
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.AD_SOUND_MUTE,param1.data));
      }
      
      private function onAdHolderShow(param1:YoukuAdEvent) : void {
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.PLUGIN_ERROR,param1.data));
      }
      
      private function onAdFrontReadyOrError(param1:YoukuAdEvent) : void {
         if(param1.data)
         {
            PlayerConfig.bctime = param1.data.adtime;
         }
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.AD_READY_OR_ERROR));
      }
      
      private function onAdBlockReport(param1:YoukuAdEvent) : void {
         this._playerProxy.dispatchEvent(new ReportEvent(ReportEvent.AD_BLOCK_REPORT,param1.data));
      }
      
      private function onAdBlockNoticeReport(param1:YoukuAdEvent) : void {
         this._playerProxy.dispatchEvent(new ReportEvent(ReportEvent.AD_BLOCK_NOTICE_REPORT,param1.data));
      }
      
      private function onAdVideoPlayComplete(param1:YoukuAdEvent) : void {
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.AD_PLAY_COMPLETE));
      }
      
      private function onAdFrontLoadComplete(param1:YoukuAdEvent) : void {
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.AD_LOAD_COMPLETE));
      }
      
      private function onAdFrontStartToPlay(param1:YoukuAdEvent) : void {
         if(PlayerConfig.playListData.share == true && PlayerConfig.playListData.share_type == "ad")
         {
            this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.AD_SHARE_SHOW));
         }
         if(PlayerConfig.playListData.user.vipnotes != null)
         {
            this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.AD_VIP_SHARE_SHOW));
         }
      }
      
      private function onAdLoadTimeReport(param1:YoukuAdEvent) : void {
         switch(param1.type)
         {
            case YoukuAdEvent.AD_LOAD_TIME:
               if(param1.data.type == LoadTimeData.TYPE_AD_FRONT_REQUEST)
               {
                  PlayerConfig.loadtimeData.ltadrequest_start = this._adContext.ltadrequest_start;
                  PlayerConfig.loadtimeData.ltadrequest_end = this._adContext.ltadrequest_end;
                  PlayerConfig.loadtimeData.ltadrequest = PlayerConfig.loadtimeData.ltadrequest_end - PlayerConfig.loadtimeData.ltadrequest_start;
               }
               else if(param1.data.type == LoadTimeData.TYPE_CRAZY)
               {
                  PlayerConfig.loadtimeData.ltcrazy_start = this._adContext.ltcrazy_start;
                  PlayerConfig.loadtimeData.ltcrazy_end = this._adContext.ltcrazy_end;
                  PlayerConfig.loadtimeData.ltcrazy = PlayerConfig.loadtimeData.ltcrazy_end - PlayerConfig.loadtimeData.ltcrazy_start;
               }
               else if(param1.data.type == LoadTimeData.TYPE_AD)
               {
                  PlayerConfig.loadtimeData.ltadview_start = this._adContext.ltadview_start;
                  PlayerConfig.loadtimeData.ltadview_end = this._adContext.ltadview_end;
                  PlayerConfig.loadtimeData.ltadview = PlayerConfig.loadtimeData.ltadview_end - PlayerConfig.loadtimeData.ltadview_start;
               }
               
               
               this._playerProxy.dispatchEvent(new ReportEvent(ReportEvent.LOAD_TIME_REPORT,param1.data));
               break;
            case YoukuAdEvent.AD_LOAD_TIME_REQUEST:
               PlayerConfig.loadtimeData.ltadrequest_start = this._adContext.ltadrequest_start;
               PlayerConfig.loadtimeData.ltadrequest_end = this._adContext.ltadrequest_end;
               PlayerConfig.loadtimeData.ltadrequest = this._adContext.ltadrequest;
               this._playerProxy.dispatchEvent(new ReportEvent(ReportEvent.PLAYER_LOAD_TIME_EVENT,param1.data));
               break;
         }
      }
      
      private function onPlayerRequestPlayAD(param1:PlayerControlEvent) : void {
         var _loc2_:* = false;
         if((param1.data) && (param1.data.skip) && param1.data.skip == true)
         {
            _loc2_ = true;
         }
         this.updataAdContext();
         this._adManager.loadFront(_loc2_);
      }
      
      private function onPlayerSizeChanged(param1:PlayerControlEvent) : void {
         var _loc2_:Number = param1.data.width;
         var _loc3_:Number = param1.data.height;
         var _loc4_:Number = param1.data.cheight;
         var _loc5_:Boolean = param1.data.fullscreen;
         this._adContext.isInFullScreen = _loc5_;
         this._adManager.setSize(_loc2_,_loc3_,_loc4_,_loc5_);
      }
      
      private function onPlayerChangeVolume(param1:PlayerControlEvent) : void {
         var _loc2_:Number = param1.data.volume;
         this._adManager.changeVolume(_loc2_);
      }
      
      private function onPlayerError(param1:PlayerControlEvent) : void {
         this._adManager.closeAd();
      }
      
      private function onPlayerReset(param1:PlayerControlEvent) : void {
         this._adManager.resetAd();
      }
      
      private function onPlayerOver(param1:PlayerControlEvent) : void {
         this._adManager.setVideoEnd();
      }
      
      private function onPlayerPlaying(param1:CoreEvent) : void {
         this._adManager.setVideoPlayingTime(param1.data.nsTime,param1.data.ST);
      }
      
      private function updataAdContext() : void {
         this._adContext.isDvdPlay = PlayerConfig.dvdPlay;
         this._adContext.allowBCSpecialType = PlayerConfig.allowBCSpecialType;
         this._adContext.hasUserData = !(this._playerProxy.playerData.userData == null);
         if(this._adContext.hasUserData)
         {
            this._adContext.vip = this._playerProxy.playerData.userData.vip;
         }
         if(this._adContext.vip)
         {
            this._adContext.uk = this._playerProxy.playerData.userData.userID;
         }
         this._adContext.vidEncoded = PlayerConfig.playListData.vidEncoded;
         this._adContext.winType = this._playerProxy.rootGetData.rootWinType;
         this._adContext.isContinuous = this._playerProxy.playerData.isContinuous;
         this._adContext.hasNextVideo = !(this._playerProxy.playerData.videoListNextVideo == null);
         this._adContext.rootShowLoop = this._playerProxy.rootGetData.rootShowLoop;
         this._adContext.isFSOLoop = this._playerProxy.fsoProxy.getLoopSetting();
         this._adContext.totalTime = PlayerConfig.playListData.totalTime;
         this._adContext.isInFullScreen = this._playerProxy.playerData.isInFullScreen;
         this._adContext.ct = PlayerConfig.playListData.ct;
         this._adContext.cs = PlayerConfig.playListData.cs;
         this._adContext.hasShowData = !(PlayerConfig.playListData.show == null);
         if(this._adContext.hasShowData)
         {
            this._adContext.isVideoPaid = PlayerConfig.playListData.show.isVideoPaid;
            this._adContext.showID = PlayerConfig.playListData.show.showID;
         }
         this._adContext.sourceVid = PlayerConfig.playListData.sourceVid;
         this._adContext.sid = PlayerConfig.playListData.sid;
         this._adContext.u = PlayerConfig.playListData.userId;
         this._adContext.videoId = PlayerConfig.playListData.videoId;
         this._adContext.partnerId = PlayerConfig.rootData.partnerId;
         this._adContext.defaultQuality = this.getDefaultQuality();
         this._adContext.hasPartnerData = !(this._playerProxy.partnerData == null);
         if(this._adContext.hasPartnerData)
         {
            this._adContext.atm = this._playerProxy.partnerData.atm;
         }
         this._adContext.embedId = PlayerConfig.rootData.embedId;
         this._adContext.tags = PlayerConfig.playListData.tags;
         this._adContext.adext = PlayerConfig.rootData.adext;
         this._adContext.ev = PlayerConfig.rootData.ev;
         this._adContext.hasFolder = !(PlayerConfig.playListData.folder == null);
         if(this._adContext.hasFolder)
         {
            this._adContext.folderId = PlayerConfig.playListData.folder.folderId;
         }
         this._adContext.hasTrial = !(PlayerConfig.playListData.trial == null);
         if(this._adContext.hasTrial)
         {
            this._adContext.trialType = PlayerConfig.playListData.trial.type;
         }
         this._adContext.isTudouPlayer = PlayerConfig.isTudouPlayer;
         this._adContext.screenMode = this._playerProxy.fsoProxy.getScreenMode();
         this._adContext.mute = this._playerProxy.fsoProxy.getMute();
         this._adContext.volume = this._playerProxy.fsoProxy.getVolume();
         if(PlayerConfig.playListData.verifyUser != null)
         {
            this._adContext.verifyUser = 
               {
                  "isHaveData":PlayerConfig.playListData.verifyUser.isHaveData,
                  "isHaveNotes":PlayerConfig.playListData.verifyUser.isHaveNotes
               };
         }
         this._adContext.isPaike = this._playerProxy.playerData.paike;
         this._adContext.videoIdUncode = this._playerProxy.playerData.videoIdUncode;
         this._adContext.HIGHVERSION = true;
         this._adContext.LOWVERSION = false;
         this._adContext.title = PlayerConfig.playListData.title;
      }
      
      private function getDefaultQuality() : String {
         var _loc1_:String = this._playerProxy.fsoProxy.getDefaultQuality();
         var _loc2_:* = "flv";
         if(_loc1_ == PlayerConstant.QUALITY_AUTO)
         {
            _loc2_ = "auto";
         }
         else if(_loc1_ == PlayerConstant.QUALITY_LOW)
         {
            _loc2_ = "flv";
         }
         else if(_loc1_ == PlayerConstant.QUALITY_MID)
         {
            _loc2_ = "mp4";
         }
         else if(_loc1_ == PlayerConstant.QUALITY_HIGH || _loc1_ == PlayerConstant.QUALITY_1080P)
         {
            _loc2_ = "hd2";
         }
         
         
         
         return _loc2_;
      }
   }
}
